/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.drivers;

/** 
 * A class defining a
 *
 * @author Fuina, Jamile & Pitangueira, Roque
 * @since March 2005
 */
public class EquilibriumPath implements Solution {    
    private int numMaxSteps;    
    private Step step;
    private IteractiveStrategy[] iteracStrategy;
    protected double changeLoadFactor;
    protected double changeTolerance;
    protected int changeNumMaxIterations;
    
    private int currentStep;    
	/**
	 * The constructor method
	 * @param numMaxSteps the maximum number of steps
	 * @param numMaxIterations the maximum number of iteration
	 * @param loadFactor the specified first load factor
	 * @param tolerance the tolerance to check the convergence
	 */
	public EquilibriumPath(int numMaxSteps, Step sp, IteractiveStrategy[] is, double clf, double ctol, int cnmi) {
		this.numMaxSteps = numMaxSteps;
		this.step = sp;
		this.iteracStrategy = is;
		this.changeLoadFactor = clf;
		this.changeTolerance = ctol;
		this.changeNumMaxIterations = cnmi;
	}    

	/**
	 * @param changeNMI the new umber of maximum iteration
	 */
	public void setchangeNumMaxIterations(int changeNMI){
		changeNumMaxIterations = changeNMI;
	}		
	
	/**
	 * @return the current maximum number of iterations
	 */
	public int getchangeNumMaxIterations(){
	  return changeNumMaxIterations;
	}		
	
	public void setchangeTolerance(double changeTol){
	  	changeTolerance = changeTol;
	}		
	  
	public double getchangeTolerance(){
	  	 return changeTolerance;
	  }	

    public void setchangeLoadFactor(double changeLF){
  	 changeLoadFactor = changeLF;
  	}	
  
  	public double getchangeLoadFactor(){
  	
  	 return changeLoadFactor;
  	}	

  	public int getCurrentStep(){
    	 return currentStep;
    }
    
    public void setNumMaxSteps(int numMS){
    	numMaxSteps = numMS;
    }
 
    public void setIteractiveStrategy(IteractiveStrategy[] itestr){
    	iteracStrategy = itestr;
    }
    
    public void setNumMaxInterations(int nmi){
    	this.step.setNumMaxIterations(nmi);
    }
	
    public void setTolerance(double tol){
    	this.step.setTolerance(tol);
    }

    public void setStep(Step sp){
    	this.step = sp;
    }
    
    public void execute(){
    	int cont = 0;
    	currentStep = 1;
    	step.setIteractiveStrategy(iteracStrategy[cont]);	// Primeira estrat�gia adotada
    	do{
    		step.execute();
    		if (!(step.getConvergence())){
    			if(cont < iteracStrategy.length)
    				step.setIteractiveStrategy(iteracStrategy[cont++]);
    				step.getIteractiveStrategy().setLoadFactor(this.changeLoadFactor);
    				step.setNumMaxIterations(this.changeNumMaxIterations);
    				step.setTolerance(this.changeTolerance);
    		}
    		else{
    			currentStep++;
    			step.getDriver().updateState();		
    		}
    	}
    	while(currentStep <= numMaxSteps);	
    }   	
}
